package com.ce.pms.charge.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ce.pms.base.service.impl.PmsBaseServiceImpl;
import com.ce.pms.charge.dao.IPeriodDao;
import com.ce.pms.charge.model.PeriodDomain;
import com.ce.pms.charge.service.IPeriodService;
import com.ce.pms.charge.vo.PeriodVO;
import com.ce.pms.common.model.SysInfo;

import com.ce.pms.house.dao.IHouseDao;
import com.ce.pms.house.model.ChargePrecinctDomain;
import com.ce.pms.house.model.Precinct;
import com.ce.pms.house.vo.ChargePrecinctVO;
import com.ce.util.BlankUtil;
import com.ce.webframework.sec.vo.SecUserinfoVO;

public class PeriodServiceImpl extends PmsBaseServiceImpl implements IPeriodService {

	private IPeriodDao periodDao ;
	public IPeriodDao getPeriodDao() {
		return periodDao;
	}
	public void setPeriodDao(IPeriodDao periodDao) {
		this.periodDao = periodDao;
	}
	/**
	 * 功能描述: 根据收费周期状态查询收费周期 <br>
	 * @param validity   0:无效(已月结,按收费周期分组)
	 * 					 1:有效(未月结不分组)
	 * 					 null:所有的收费周期，并以收费周期分组
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-8-29  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public List<PeriodVO> findPeriodByValidity(String validity) {
		List<PeriodDomain> temp = periodDao.findPeriodByValidity(validity);



		List<PeriodVO> list = new ArrayList<PeriodVO>();
		for (PeriodDomain domain : temp) {
			list.add(this.periodDOToVO(domain));
		}

		return list;
	}

	/**
	 * 功能描述:取得当前收费周期，与权限配合使用才有实际意义<br>
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2008-9-16<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public PeriodVO queryCurrentPeriod(){
		return periodDOToVO(periodDao.queryCurrentPeriod());
	}
	
	/**
	 * 功能描述:根据id查询收费周期  <br>
	 * @param periodId
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2009-1-7  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public PeriodDomain getPeriodDomainById(Long periodId){
		return this.periodDao.loadById(periodId);
	}


	/**
	 * 方法描述: 根据房屋ＩＤ查询其对应的收费管理区下的当前有效收费周期方法
	 * @author 胡煦
	 * Create on:2008-9-4
	 * @param houseId
	 * @return PeriodDomain
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public PeriodDomain findPeriodByHouseId(Long houseId){
		return periodDao.findAvailPeriodBy(houseDao.findById(houseId).getBuilding().getKeyword());
	}

	private IHouseDao houseDao;
	/**
	 * @param houseDao the houseDao to set
	 */
	public void setHouseDao(IHouseDao houseDao) {
		this.houseDao = houseDao;
	}

	/**
	 * 功能描述:根据收费管理区查询收费周期   <br>
	 * @param PrecinctId  管理区ID
	 * @param validity  收费周期有效性 0:无效  1:有效  null:查询全部
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-9-4  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public List<PeriodVO> findPeriodByPrecinct(Long PrecinctId, String validity) {
		if (BlankUtil.isBlank(PrecinctId)) {
			return null;
		}
		List<PeriodDomain> temp =periodDao.findPeriodByPrecinct(PrecinctId, validity);

		List<PeriodVO> list = new ArrayList<PeriodVO>();
		for (PeriodDomain domain : temp) {
			list.add(this.periodDOToVO(domain));
		}
		return list;
	}

	/**
	 * 功能描述:收费周期DO 转 VO  <br>
	 * @param periodDomain
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-9-9  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private PeriodVO periodDOToVO (PeriodDomain periodDomain){
		PeriodVO periodVO = new PeriodVO();
		if (BlankUtil.isBlank(periodDomain))
			return null;
		periodVO.setKeyword(periodDomain.getKeyword()); //收费周期ID
		periodVO.setPrecinctID(periodDomain.getChargePrecinct()== null? null:periodDomain.getChargePrecinct().getKeyword());//管理区ID
		periodVO.setPrecinctName(periodDomain.getChargePrecinct()== null? null:periodDomain.getChargePrecinct().getName());//收费管理区
		periodVO.setPeriod(periodDomain.getPeriod()== null ? null: periodDomain.getPeriod());//收费周期
		periodVO.setStartDate(periodDomain.getStartDate() == null ? null : periodDomain.getStartDate()); //开始日期
		periodVO.setEndDate(periodDomain.getEndDate() == null ? null:periodDomain.getEndDate());//结束日期
		periodVO.setValidity(periodDomain.getValidity() == null ? null:periodDomain.getValidity());//是否有效
		return periodVO;
	}
}
